﻿using System;
using System.Collections.Generic;
using System.IO;

namespace xycn.localfilehelper
{
    /// <summary>
    /// 
    /// </summary>
    public class LocalFileHelperFactory : IPluginFactory.IPluginFactory
    {
        /// <summary>
        /// 创建IDbService对象接口
        /// </summary>
        /// <returns></returns>
        public override IFileHelper.IFileHelper CreatePlugin()
        {
            LocalFileHelper ret = new LocalFileHelper();

            return (IFileHelper.IFileHelper)ret;
        }
    }

    /// <summary>
    /// 本地文件存储操作
    /// </summary>
    public class LocalFileHelper : IFileHelper.IFileHelper
    {
        /// <summary>
        /// 用户名、密码、验证码登陆文件系统
        /// </summary>
        /// <param name="user_name"></param>
        /// <param name="pswd"></param>
        /// <param name="verify_code"></param>
        /// <returns></returns>
        public int Login(string user_name, string pswd, string verify_code)
        {
            return 0;
        }

        /// <summary>
        /// 使用指定证书文件登录文件系统
        /// </summary>
        /// <param name="certifcate_path"></param>
        /// <returns></returns>
        public int Login(string certifcate_path)
        {
            return 0;
        }

        /// <summary>
        /// 使用证书登录文件系统
        /// </summary>
        /// <param name="certifcate_content"></param>
        /// <returns></returns>
        public int Login(byte[] certifcate_content)
        {
            return 0;
        }

        /// <summary>
        /// 登出文件系统
        /// </summary>
        public void Logout()
        {
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="dir">待创建目录名称</param>
        /// <returns></returns>
        public int MkDir(string dir)
        {
            int ret = 0;



            return ret;
        }

        /// <summary>
        /// 删除文件或目录
        /// </summary>
        /// <param name="path">当path为某个文件时，删除该文件，当path为目录时，删除目录及目录中的文件</param>
        /// <returns></returns>
        public int Rm(string path)
        {
            int ret = 0;



            return ret;
        }

        /// <summary>
        /// 拷贝文件或目录，将文件或目录拷贝为其他目录或文件
        /// </summary>
        /// <param name="source_path">源文件，带路经文件名或目录名称</param>
        /// <param name="dest_path">目标文件，带路经文件名或目录名称</param>
        /// <returns></returns>
        public int Copy(string source_path, string dest_path)
        {
            int ret = 0;



            return ret;
        }
        /// <summary>
        /// 进入某个目录
        /// </summary>
        /// <param name="dir">目录名称</param>
        /// <returns></returns>
        public int Cd(string dir)
        {
            int ret = 0;



            return ret;
        }

        /// <summary>
        /// 列举某个目录中的文件
        /// </summary>
        /// <param name="dir">指定目录，当dir为空，默认为当前目录</param>
        /// <returns></returns>
        public List<string> LsFile(string dir = null)
        {
            List<string> ret = new List<string>();



            return ret;
        }

        /// <summary>
        /// 列举某个目录中的子目录
        /// </summary>
        /// <param name="dir"></param>
        /// <returns>指定目录，当dir为空，默认为当前目录</returns>
        public List<string> LsDir(string dir = null)
        {
            List<string> ret = new List<string>();



            return ret;
        }

        /// <summary>
        /// 判断指定文件或路径是否存在
        /// </summary>
        /// <param name="path">带路经文件名或目录名称</param>
        /// <returns></returns>
        public bool Exists(string path)
        {
            bool ret = false;

            ret = File.Exists(path);

            return ret;
        }

        /// <summary>
        /// 以共享只读方式打开目录
        /// </summary>
        /// <param name="path">带路经文件名</param>
        /// <returns></returns>
        public Stream OpenRead(string path)
        {
            if (!this.Exists(path)) return null;
            
            Stream ret = new System.IO.FileStream(path,
                FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            
            return ret;
        }

        /// <summary>
        /// 打开文件，当文件不存在，自动创建
        /// 注：linux系统下，同一文件可被同时打开多次进行读写；
        /// windows操作系统下，打开为独占模式
        /// </summary>
        /// <param name="path">带路经文件名</param>
        /// <returns></returns>
        public Stream OpenWrite(string path)
        {
            Stream ret = new FileStream(path, FileMode.OpenOrCreate);
            return ret;
        }
    }
}
